package com.xjcs.saplingplan.ui.activity.countdown

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.ItemTouchHelper
import com.jeremyliao.liveeventbus.LiveEventBus
import com.xjcs.saplingplan.adapter.CountdownCateAdapter
import com.xjcs.saplingplan.adapter.RecyclerViewItemDecoration
import com.xjcs.saplingplan.base.BaseVbActivity
import com.xjcs.saplingplan.const.EventBusConstants
import com.xjcs.saplingplan.const.setSingleClick
import com.xjcs.saplingplan.databinding.ActivityPlanCateBinding
import com.xjcs.saplingplan.datastore.AppData
import com.xjcs.saplingplan.dialog.BaseCenterPop
import com.xjcs.saplingplan.room.bean.countdown.PlanCateBean
import com.xjcs.saplingplan.room.utils.PlanCateRoomUtils
import com.xjcs.saplingplan.ui.callback.ItemTouchHelperCallback
import com.xjcs.saplingplan.ui.viewmodel.CountdownViewModel
import com.xjcs.saplingplan.utils.DialogUtils

class CountdownCateActivity : BaseVbActivity<ActivityPlanCateBinding>() {

    companion object {
        fun startActivity(context: Context) {
            val intent = Intent(context, CountdownCateActivity::class.java)
            context.startActivity(intent)
        }
    }

    private lateinit var countdownCateAdapter: CountdownCateAdapter
    private val userId = AppData.getUserId()

    private val mViewModel: CountdownViewModel by viewModels()

    override fun getViewBinding(): ActivityPlanCateBinding {
        return ActivityPlanCateBinding.inflate(layoutInflater)
    }

    override fun initUi(savedInstanceState: Bundle?) {
        initRecyclerView()
        mViewModel.getCountdownCateList(userId)
    }

    private fun initRecyclerView() {
        countdownCateAdapter = CountdownCateAdapter(this, mutableListOf(), {
            //拖动完成插入数据库
            PlanCateRoomUtils.insertList(it)
        }, { pos, item ->
            showDeletePop(pos, item)
        })
        mBinding.recyclerView.adapter = countdownCateAdapter
        mBinding.recyclerView.addItemDecoration(
            RecyclerViewItemDecoration(this, dividerHeight = 5, dividerColor = Color.TRANSPARENT)
        )
        //设置可拖动排序
        val itemCallback = ItemTouchHelperCallback(this, countdownCateAdapter)
        ItemTouchHelper(itemCallback).attachToRecyclerView(mBinding.recyclerView)
    }

    override fun registerObserver() {
        LiveEventBus.get<String>(EventBusConstants.REFRESH_COUNTDOWN_CATE).observe(this){
            mViewModel.getCountdownCateList(userId)
        }

        mViewModel.countdownCateList.observe(this) {
            countdownCateAdapter.setData(it)
        }
    }

    override fun initListener() {
        mBinding.ivBack.setSingleClick {
            onBackPressedDispatcher.onBackPressed()
        }
        mBinding.viewCreate.setSingleClick {
            CountdownCateCreateActivity.startActivity(this)
        }
    }

    private fun showDeletePop(pos: Int, item: PlanCateBean) {
        DialogUtils.showBasePop(this, outDismiss = true, showBg = true)
            .asCustom(BaseCenterPop(this, "确定要删除分类", "删除分类后，该分类下的倒数日在全部中查看"){
                // 从数据源中移除该项
                countdownCateAdapter.getData().removeAt(pos)
                // 通知Adapter数据变化
                countdownCateAdapter.notifyItemRemoved(pos)
                //删除
                PlanCateRoomUtils.deletePlanCateBean(item)
            })
    }

}